package com.mirror.llk;

import java.util.Timer;
import java.util.TimerTask;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import cn.waps.AdView;

import com.badlogic.gdx.backends.android.AndroidApplication;
import com.mirror.llk.bean.domain.GameBean;
import com.mirror.llk.view.GameView;

/**
 * 游戏主页面Activity.
 * <p>
 * Copyright: Copyright (c) 2013-7-10 上午10:14:51
 * <p>
 * Company: DIY工作室
 * <p>
 * Author: 李晖
 * <p>
 * Version: 1.0
 * <p>
 */
public class GameMainActivity extends AndroidApplication {

	private GameView gameView;

	private Button pauseBtn;

	private ImageButton tipBtn, refreshBtn;

	private TextView text_refresh_num;

	private TextView text_tip_num;

	private LinearLayout gameContainer, adsGameContainer;

	private ProgressBar pbRemainingTime;

	private TextView tvRemainingTime;

	private int addTime = 0;

	private boolean clearPbRemainingTime = false;

	private int totalRemainingTime;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.game_view);
		GameBean gameBean = new GameBean();
		gameBean.setType(getIntent().getIntExtra("gameType", 1));
		gameBean.setThemeId(getIntent().getIntExtra("themeId", 1));
		gameBean.setHistoryId(getIntent().getIntExtra("historyId", 0));

		// 互动广告调用方式
		LinearLayout layout = (LinearLayout) this.findViewById(R.id.adsContainer);
		new AdView(this, layout).DisplayAd(10);

		DisplayMetrics dm = new DisplayMetrics();
		this.getWindowManager().getDefaultDisplay().getMetrics(dm);
		int width = dm.widthPixels;

		// 初始化游戏区域
		gameContainer = (LinearLayout) this.findViewById(R.id.gameContainer);
		gameContainer.getLayoutParams().height = width / 8 * 10;

		adsGameContainer = (LinearLayout) this.findViewById(R.id.adsGameContainer);
		adsGameContainer.getLayoutParams().height = width / 8 * 10;
		gameView = new GameView(GameMainActivity.this, gameBean);
		View view = initializeForView(gameView, false);
		gameContainer.addView(view);

		pbRemainingTime = (ProgressBar) this.findViewById(R.id.pbRemainingTime);
		tvRemainingTime = (TextView) this.findViewById(R.id.tvRemainingTime);

		pauseBtn = (Button) this.findViewById(R.id.pauseBtn);

		tipBtn = (ImageButton) this.findViewById(R.id.tip_btn);
		text_tip_num = (TextView) this.findViewById(R.id.text_tip_num);
		refreshBtn = (ImageButton) this.findViewById(R.id.refresh_btn);
		text_refresh_num = (TextView) this.findViewById(R.id.text_refresh_num);
		pauseBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				boolean status = gameView.isGameStatus();
				gameView.setGameStatus(!status);
				if (status) {
					gameView.pause();
					pauseBtn.setText("继续");
					gameContainer.setVisibility(View.GONE);
					adsGameContainer.setVisibility(View.VISIBLE);
				} else {
					gameView.resume();
					pauseBtn.setText("暂停");
					gameContainer.setVisibility(View.VISIBLE);
					adsGameContainer.setVisibility(View.GONE);
				}

			}
		});
		tipBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Animation shake01 = AnimationUtils.loadAnimation(GameMainActivity.this, R.anim.shake);
				tipBtn.startAnimation(shake01);
				if (gameView.tip())
					messageLoadHandler.sendEmptyMessage(2);
			}
		});

		refreshBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				notifyChangeNum(true);
			}
		});

		startTimer();
	}

	/** 加载消息定时器 */
	private Timer messageLoadTimer = null;

	/**
	 * 启动定时器
	 */
	private void startTimer() {
		messageLoadTimer = new Timer();
		messageLoadTimer.schedule(new TimerTask() {

			public void run() {
				Message message = new Message();
				message.what = 1;
				// 通过handle更新UI
				messageLoadHandler.sendMessage(message);
			}
		}, 10, 1000);
	}

	/** 加载消息handle */
	@SuppressLint("HandlerLeak")
	private Handler messageLoadHandler = new Handler() {

		public void handleMessage(Message msg) {
			switch (msg.what) {
				case 1:
					update();
					break;
				case 2:
					text_tip_num.setText("" + gameView.getHelpNum());
					break;
				case 3:
					refreshBtn.startAnimation(AnimationUtils.loadAnimation(GameMainActivity.this, R.anim.shake));
					text_refresh_num.setText("" + gameView.getRefreshNum());
					break;
			}
			super.handleMessage(msg);
		}

		void update() {
			if (gameView.isGameStatus()) {
				int newTime = Integer.valueOf(tvRemainingTime.getText().toString()) - 1 + addTime;
				addTime = 0;
				tvRemainingTime.setText(newTime + "");
				Log.e("lihui", newTime + "/" + totalRemainingTime);
				pbRemainingTime.setProgress(newTime * 100 / totalRemainingTime);
				if (newTime == 0) {
					gameView.overGame();
				}
			}

			if (isClearPbRemainingTime()) {
				setClearPbRemainingTime(false);
				tvRemainingTime.setText(totalRemainingTime + "");
				pbRemainingTime.setProgress(100);
			}
		}
	};

	@Override
	protected void onDestroy() {
		// 停止timer
		if (messageLoadTimer != null) {
			messageLoadTimer.cancel();
			messageLoadTimer = null;
		}
		super.onDestroy();
	}

	public int getAddTime() {
		return addTime;
	}

	public void setAddTime(int addTime) {
		this.addTime = addTime;
	}

	public boolean isClearPbRemainingTime() {
		return clearPbRemainingTime;
	}

	public void setClearPbRemainingTime(boolean clearPbRemainingTime) {
		this.clearPbRemainingTime = clearPbRemainingTime;
	}

	public int getTotalRemainingTime() {
		return totalRemainingTime;
	}

	public void setTotalRemainingTime(int totalRemainingTime) {
		this.totalRemainingTime = totalRemainingTime;
	}

	/**
	 * @param isDo
	 *            是否做洗牌
	 * @return
	 */
	public boolean notifyChangeNum(boolean isDo) {
		boolean flag = true;
		if (isDo) {
			flag = gameView.change();
		}
		if (flag) {
			messageLoadHandler.sendEmptyMessage(3);
		}
		return flag;
	}

	public void notifyHelpNum() {
		messageLoadHandler.sendEmptyMessage(2);
	}
}